const http = require('../../../utils/http.js');
Page({
  data: {
    storeInfo: {
      auth_type: 0,
      logo: '',
      name: '',
      classify_id: '',
      classify_name: '',
    },
    pickerValue: [0],
    infolist: [],
    status: 0,
    imageUrl: ''
  },
  onLoad: function (options) {
    var _this = this
    _this.onPageInfo('1')
  },
  onReady: function () {},
  onShow: function () {
    var _this = this,
      storeInfo = _this.data.storeInfo
    if (_this.data.status == 1) {
      wx.getFileSystemManager().readFile({
        filePath: _this.data.imageUrl, //选择图片返回的相对路径
        encoding: 'base64', //编码格式
        success: rep => { //成功的回调
          console.log('data:image/png;base64,' + rep.data)
          storeInfo.logo = 'data:image/png;base64,' + rep.data
          _this.setData({
            pagelogo: _this.data.imageUrl,
            storeInfo: storeInfo
          })
        }
      })
    }
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  /**
   * 获取基本信息
   */
  onPageInfo(e) {
    var _this = this,
      type = e
    if (e == '1') {
      http.getRequest('v1/store/create', {},
        function (res) {
          console.log(res)
          _this.setData({
            infolist: res.data
          })
        },
        function (err) {})
    } else {
      var data = _this.data.storeInfo
      if (!data.logo || !data.name || !data.classify_id || !data.classify_name) {
        wx.showToast({
          title: '请填写完整信息',
          icon: 'none'
        })
        return false
      }
      http.postRequest('v1/store', data,
        function (rep) {
          console.log(rep)
          if (rep.code == 201) {
            http.postRequest('syncData', {
                is_sync: 1
              },
              function (res) {
                var member = wx.getStorageSync('member')
                member = res.data
                wx.setStorageSync('member', member)
                getApp().globalData.token = res.data.token;
                wx.showToast({
                  title: '开通成功',
                  icon: 'success'
                })
                wx.setStorageSync('app_id', rep.data.app_url.appid)
                wx.setStorageSync('page_path', rep.data.app_url.url_path)
                setTimeout(() => {
                  wx.navigateTo({
                    url: '/pages/onNewPages/onOpenStroeSuccess/onOpenStroeSuccess?app_id=' + rep.data.app_url.appid + '&page_path=' + rep.data.app_url.url_path,
                  })
                }, 1000);
              },
              function (res) {})
          }
        },
        function (err) {
          wx.showToast({
            title: err.msg,
            icon: 'none'
          })
        })
    }

  },
  onUserCententInfo(e) {
    /**
     * 用户填写信息搜集方法
     * id:1   /店铺头像上传方法
     * id:2   //店铺名称获取
     * id:3   //店铺类目
     * id:4   //是否是个人企业
     */
    var _this = this,
      id = e.currentTarget.id,
      storeInfo = _this.data.storeInfo
    if (id == 1) {
      wx.navigateTo({
        url: '/pages/onNewPages/upLoadImage/upLoadImage',
      })
      // wx.chooseImage({
      //   count: 1,
      //   success: res => {
      //     wx.getFileSystemManager().readFile({
      //       filePath: res.tempFilePaths[0], 
      //       encoding: 'base64', 
      //       success: rep => { 
      //         console.log('data:image/png;base64,' + rep.data)
      //         storeInfo.logo = 'data:image/png;base64,' + rep.data
      //         _this.setData({
      //           pagelogo: res.tempFilePaths[0]
      //         })
      //       }
      //     })
      //   }
      // })
    }
    if (id == 2) {
      storeInfo.name = e.detail.value
    }
    if (id == 3) {
      var listinfo = _this.data.infolist.classify_list
      storeInfo.classify_id = listinfo[Number(e.detail.value)].classify_id
      storeInfo.classify_name = listinfo[Number(e.detail.value)].name
    }
    if (id == 4) {
      storeInfo.auth_type = 0
    }
    if (id == 5) {
      storeInfo.auth_type = 1
    }
    _this.setData({
      storeInfo: storeInfo
    })
  }
})